#include "FieldWindow.h"

#include <QtGui>

FieldWindow::FieldWindow()
{
  QDoubleValidator *sci = new QDoubleValidator();

  QComboBox *pulseType = new QComboBox();
  pulseType->addItem("Constant Field");
  pulseType->addItem("Square Pulse");

  QWidget *constantWidget = new QWidget();
  QFormLayout *constantLayout = new QFormLayout();
  constantAmplitude = new QLineEdit();
  constantAmplitude->setValidator(sci);
  constantAmplitude->setToolTip("field amplitude in Teslas");
  constantLayout->addRow("B=",constantAmplitude);
  constantWidget->setLayout(constantLayout);

  QWidget *squareWidget = new QWidget();
  QFormLayout *squareLayout = new QFormLayout();
  amplitude = new QDoubleSpinBox();
  amplitude->setEnabled(false);
  length = new QDoubleSpinBox();
  length->setEnabled(false);
  squareLayout->addRow("B=",amplitude);
  squareLayout->addRow("length=",length);
  squareWidget->setLayout(squareLayout);
  QStackedWidget *trajWidget = new QStackedWidget();
  trajWidget->addWidget(constantWidget);
  trajWidget->addWidget(squareWidget);

  QWidget *loopWidget = new QWidget();
  QFormLayout *loopLayout = new QFormLayout();
  max = new QLineEdit();
  max->setToolTip("maximum value of field in Teslas");
  max->setValidator(sci);
  min = new QLineEdit();
  min->setValidator(sci);
  min->setToolTip("minimum value of field in Teslas");
  step = new QLineEdit();
  step->setValidator(sci);
  step->setToolTip("value of the field step in Teslas");
  loopLayout->addRow("min",min);
  loopLayout->addRow("max",max);
  loopLayout->addRow("step",step);
  loopWidget->setLayout(loopLayout);

  layout = new QStackedWidget();
  layout->addWidget(trajWidget);
  layout->addWidget(loopWidget);

  QGroupBox *angleGroup = new QGroupBox("Angles");
  QFormLayout *angleLayout = new QFormLayout();
  theta = new QDoubleSpinBox();
  theta->setToolTip("angle between the z and y axis in degrees");
  phi = new QDoubleSpinBox();
  phi->setToolTip("angle between the x and y axis in degrees");
  angleLayout->addRow("theta=",theta);
  angleLayout->addRow("phi=",phi);
  QFormLayout *angleLoop = new QFormLayout();
  thetaMin = new QDoubleSpinBox();
  thetaMax = new QDoubleSpinBox();
  thetaStep = new QDoubleSpinBox();
  angleLoop->addRow("theta min",thetaMin); 
  angleLoop->addRow("theta max",thetaMax);
  angleLoop->addRow("theta Step",thetaStep);
  angleWidget = new QWidget();
  angleWidget->hide();
  angleWidget->setLayout(angleLoop);
  QHBoxLayout *angleHLayout = new QHBoxLayout();
  angleHLayout->addLayout(angleLayout);
  angleHLayout->addWidget(angleWidget);
  angleGroup->setLayout(angleHLayout);

  QVBoxLayout *mainLayout = new QVBoxLayout(this);
  mainLayout->addWidget(pulseType);
  mainLayout->addWidget(layout);
  mainLayout->addWidget(angleGroup);

  QObject::connect(pulseType,SIGNAL(activated(int)),trajWidget,SLOT(setCurrentIndex(int))); 
}

FieldWindow::~FieldWindow()
{

}

NewParameters FieldWindow::getParameters(NewParameters params)
{
  QMap<QString,double> fieldParams;
  fieldParams["value"]=constantAmplitude->text().toFloat();
  fieldParams["theta"]=theta->text().toFloat();
  fieldParams["phi"]=phi->text().toFloat();
  fieldParams["max"]=max->text().toFloat();
  fieldParams["min"]=min->text().toFloat();
  fieldParams["step"]=step->text().toFloat();
  fieldParams["thetaMin"]=thetaMin->value();
  fieldParams["thetaMax"]=thetaMax->value();
  fieldParams["thetaStep"]=thetaStep->value();
  params.setField(fieldParams);
  return params;
}

void FieldWindow::showTraj()
{
  QTextStream cout(stdout,QIODevice::WriteOnly);
  cout<<"show field traj"<<endl;
  angleWidget->hide();
  layout->setCurrentIndex(0);
}
void FieldWindow::showLoop()
{
  QTextStream cout(stdout,QIODevice::WriteOnly);
  cout<<"show field loop"<<endl;
  angleWidget->hide();
  layout->setCurrentIndex(1);
}
void FieldWindow::showAstroid()
{
  QTextStream cout(stdout,QIODevice::WriteOnly);
  cout<<"show field astroid"<<endl;
  layout->setCurrentIndex(1);
  angleWidget->show();
}

void FieldWindow::loadParameters(NewParameters params)
{
  QMap<QString,double> fieldParams;
  QString str;
  fieldParams = params.getField();
  constantAmplitude->clear();
  max->clear();
  min->clear();
  step->clear();
  constantAmplitude->insert(str.setNum(fieldParams["value"]));
  theta->setValue(fieldParams["theta"]);
  phi->setValue(fieldParams["phi"]);
  max->insert(str.setNum(fieldParams["max"]));
  min->insert(str.setNum(fieldParams["min"]));
  step->insert(str.setNum(fieldParams["step"]));
  thetaMin->setValue(fieldParams["thetaMin"]);
  thetaMax->setValue(fieldParams["thetaMax"]);
  thetaStep->setValue(fieldParams["thetaStep"]);
}
